import { getData, removeData, setData } from '@/utils/ls'
import { loginAPI } from '@/api/user'
const TOKENKEY = 'rabbit-profile'
const user = {
  namespaced: true,
  state: () => ({
    profile: getData(TOKENKEY) || {}
  }),
  mutations: {
    setProfile (state, profile) {
      // 1. 存入vuex state
      state.profile = profile
      // 2. 本地存一份
      setData(TOKENKEY, profile)
    },
    clearProfile (state) {
      state.profile = {}
      removeData(TOKENKEY)
    }
  },
  actions: {
    async fetchLogin ({ commit }, { account, password }) {
      // 调用登录接口
      const res = await loginAPI({ account, password })
      // 提交mutation函数 把token存入state
      commit('setProfile', res.result)
    }
  }
}

export default user
